package com.ukao.cashregister.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;
import com.ukao.cashregister.R;
import com.ukao.cashregister.bean.QuiryDetailBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;

import java.util.List;

/**
 * Created by ukaodev on 2017/6/14.
 */

public class OrdersDetailAdapter extends CommonAdapter<QuiryDetailBean.ProductListBean> {




    public OrdersDetailAdapter(Context context, List<QuiryDetailBean.ProductListBean> datas) {
        super(context, datas, R.layout.clothing_list_item);
    }





    private OnItemChildClickListener mOnItemChildClickListener;

    public void setOnItemChildClickListener(OnItemChildClickListener mOnItemChildClickListener) {
        this.mOnItemChildClickListener = mOnItemChildClickListener;
    }


    @Override
    public void convert(final ViewHolder viewHolder, QuiryDetailBean.ProductListBean mProductInfo) {


        TextView hasRefund = viewHolder.getView(R.id.hasRefund);
        ImageView accessory = viewHolder.getView(R.id.order_detail_accessory);
        ImageView rightBtn = viewHolder.getView(R.id.right_btn);
        TextView picCnt = viewHolder.getView(R.id.picCnt);
        TextView productName = viewHolder.getView(R.id.productName);
        TextView statusText = viewHolder.getView(R.id.statusText);
        TextView no = viewHolder.getView(R.id.no);
        TextView total = viewHolder.getView(R.id.total);
        TextView serviceName = viewHolder.getView(R.id.serviceName);
        TextView flawDesc = viewHolder.getView(R.id.flawDesc);
        TextView addServiceDesc = viewHolder.getView(R.id.addServiceDesc);
        TextView scanCode = viewHolder.getView(R.id.scanCode);
        TextView subtotal = viewHolder.getView(R.id.subtotal);
        TextView tranTypeText = viewHolder.getView(R.id.tranTypeText);
        TextView tranNum = viewHolder.getView(R.id.tranNum);
        ImageView checkBox = viewHolder.getView(R.id.check_box);
        L.i("mProductInfo.isEnableSign()=" + mProductInfo.isEnableSign() + "mProductInfo.isEnableStockRework()=" + mProductInfo.isEnableStockRework());
       /*未付款  已付款退款金额不为0*/
        if (mProductInfo.isEnableSign() || mProductInfo.isEnableStockRework() || mProductInfo.isEnableRefund()) {
            checkBox.setVisibility(View.VISIBLE);


        } else {
            checkBox.setVisibility(View.GONE);
        }


        if (mProductInfo.isCheck()) {

            checkBox.setImageResource(R.drawable.check_box_check);
            viewHolder.itemView.setBackgroundResource(R.color.light_yellow);
        } else {

            checkBox.setImageResource(R.drawable.check_box_normal);
            viewHolder.itemView.setBackgroundResource(R.color.white);
        }
        if (!CheckUtils.isNull(mProductInfo.getPicCnt())) {
            if (mProductInfo.getPicCnt() != 0) {
                picCnt.setText("衣物照片：" + mProductInfo.getPicCnt());
            } else {
                picCnt.setText("衣物照片：" + "无");
            }

        }
        /*退洗标识*/
        if(CheckUtils.isEmpty(mProductInfo.getHasRefundText())) {
            hasRefund.setVisibility(View.GONE);

        } else {
            hasRefund.setText(mProductInfo.getHasRefundText());
            hasRefund.setVisibility(View.VISIBLE);
        }


        if (mProductInfo.getType() == 1) {/*衣物*/
            accessory.setVisibility(View.GONE);
            productName.setText(mProductInfo.getProductName());

        } else {/*附件*/
            accessory.setVisibility(View.VISIBLE);
            productName.setText("附件：" + mProductInfo.getProductName());

        }


        no.setText(mProductInfo.getNo() + "");
        scanCode.setText(mProductInfo.getScanCode());
        statusText.setText(CheckUtils.isEmptyString(mProductInfo.getStatusText()));

        total.setText(mProductInfo.getTotal() + "");
        Gson mGson = new Gson();

        String color = "";
        if (!CheckUtils.isEmpty(mProductInfo.getColorDesc())) {
            List<StringBean> mColorDesclist = mGson.fromJson(mProductInfo.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mStringBuffer = new StringBuffer();
            for (int i = 0; i < mColorDesclist.size(); i++) {
                if (i != mColorDesclist.size() - 1) {
                    mStringBuffer.append(mColorDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer.append(mColorDesclist.get(i).getName());
                }


            }
            color = mStringBuffer.toString();
        }
        String annexDesc = "";
        if (!CheckUtils.isEmpty(mProductInfo.getAnnexDesc())) {/*附件*/
            List<StringBean> mAnnexDesclist = mGson.fromJson(mProductInfo.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annexDesc = mStringBuffer3.toString();

        }
        if (!CheckUtils.isEmpty(annexDesc)) {
            annexDesc = "附件：" + annexDesc;
        }
        serviceName.setText(mProductInfo.getServiceName() + color + "￥" + CheckUtils.isEmptyNumber(mProductInfo.getSaleAmt()) + annexDesc);
        String strflawDesc = "";
        if (!CheckUtils.isEmpty(mProductInfo.getFlawDesc())) {
            List<StringBean> mflawDesclist = mGson.fromJson(mProductInfo.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mStringBuffer1 = new StringBuffer();
            for (int i = 0; i < mflawDesclist.size(); i++) {
                if (i != mflawDesclist.size() - 1) {
                    mStringBuffer1.append(mflawDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer1.append(mflawDesclist.get(i).getName());
                }


            }
            strflawDesc = mStringBuffer1.toString();

        }


        String service = "";
        if (!CheckUtils.isEmpty(mProductInfo.getAddServiceDesc())) {
            List<StringBean> mServiceDesclist = mGson.fromJson(mProductInfo.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mStringBuffer2 = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean item = mServiceDesclist.get(i);
                mStringBuffer2.append(item.getName()).append("￥").append(CheckUtils.isEmptyNumber(item.getAmt()));
            }
            service = mStringBuffer2.toString();


        }
        flawDesc.setText(strflawDesc);
        addServiceDesc.setText(service);
        scanCode.setText("条码:" + CheckUtils.isEmptyString(mProductInfo.getScanCode()));
        subtotal.setText("小计:" + CheckUtils.isEmptyNumber(mProductInfo.getSubtotal()));
        tranTypeText.setText(CheckUtils.isEmptyString(mProductInfo.getTranTypeText()));
        tranNum.setText(CheckUtils.isEmptyString(mProductInfo.getTranNum()));


        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CheckUtils.isNull(mOnItemChildClickListener)) {
                    mOnItemChildClickListener.onChildItemClick(v.getId(), getPosition(viewHolder));
                }

            }
        });
    }

}
